import groovy.json.*
import org.apache.commons.lang.StringUtils
import redis.clients.jedis.*
import com.alibaba.moco.util.*
import com.alibaba.moco.*
import script.GlobalValues

import static java.util.UUID.randomUUID

/**
 * 模拟 滴滴 登录授权认证
 * Created by wb-lj240895 on 2016/11/1.
 */
def excute(request){
    def params = request.getParameterMap();
    AuthorizeRequest req = BaseTypeUtil.mapToBean(params, AuthorizeRequest.class) as AuthorizeRequest

    Jedis redis = RedisPoolInstance.getRedisClient()
    //欢行异常判断
    def e = redis.hget(GlobalValues.KEY_EXCEPTION,"getToken")
    if (!StringUtils.isBlank(e)) {
        def slurper = new JsonSlurper()
        def eJson = slurper.parseText(e)
        return eJson
    }

    def uuid = (randomUUID() as String).replace("-","").toLowerCase()
    AuthorizeResponse response = new AuthorizeResponse()
    response.setAccess_token(uuid)
    response.setExpires_in(6400)
    response.setScope("moco")    //作用域？？
    response.setToken_type("Bearer") //类型？？
    redis.set(GlobalValues.KEY_AUTH + uuid,String.valueOf(System.currentTimeMillis()))
    redis.expire(GlobalValues.KEY_AUTH + uuid ,GlobalValues.RIDES_EXPIRE);
   /* redis.set(uuid,"true")
    redis.expire(uuid, 30000)*/
    return JsonOutput.toJson(response)

}

public class AuthorizeRequest{
    String client_id        //	string	yes	申请应用时分配的AppKey
    String client_secret    //	string	yes	申请应用时分配的AppSecret
    String grant_type       //	string	yes	请求的类型，填写client_credentials
    String phone            //	string	yes	当前用户的手机号
    String timestamp        //	int	yes	当前时间戳
    String sign             //	string	yes	签名
}
public class AuthorizeResponse {
    String access_token
    int    expires_in
    String token_type
    String scope
}
